const state = {
  goodList: [
    {
      name: '诸葛亮',
      price: 1000,
      num: 1,
      checked: false
    },
    {
      name: '蔡文姬',
      price: 1500,
      num: 1,
      checked: false
    },
    {
      name: '妲己',
      price: 2000,
      num: 1,
      checked: false
    },
    {
      name: '鲁班',
      price: 2200,
      num: 1,
      checked: false
    }
  ]
}
const mutations = {
  // 加减
  onBtnClick (state, payload) {
    const newObject = state.goodList.find((item) => item.name === payload.name)
    newObject.num = payload.num
  },
  // 删除
  del (state, payload) {
    state.goodList = state.goodList.filter((item) => item.name !== payload)
  },
  // 子组件全选传值
  changeCheck (state, payload) {
    const newObject = state.goodList.find((item) => item.name === payload.name)
    newObject.checked = payload.checked
  },
  // 父组件全选传值
  setIsAll (state, payload) {
    state.goodList.forEach(item => (item.checked = payload))
  }
}
const actions = {}
const getters = {
  // 是否全选
  isAll: state => state.goodList.every(item => item.checked === true),
  // 总价
  total (state) {
    const newList = state.goodList.filter(item => item.checked === true)
    return newList.reduce((sum, item) => sum + item.price * item.num, 0)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}